<!doctype html>
<html lang="zh-CN">
    <head>
        <meta charset="UTF-8" />
        <title>获取媒体</title>
        <script>
            function hasUserMedia() {
                return navigator.mediaDevices.getUserMedia({
                    audio: true,
                    video: true,
                });
            }

            if (hasUserMedia()) {
                navigator.mediaDevices
                    .getUserMedia({
                        audio: true,
                        video: { width: 480, height: 360 },
                    })
                    .then(function (stream) {
                        console.log(stream);
                        var first = document.getElementById("first");
                        var videoTracks = stream.getVideoTracks();
                        window.stream = stream; // make variable available to browser console
                        first.srcObject = stream;
                    })
                    .catch(function (err) {
                        console.error(err.name + err.message);
                    });
            }
        </script>
    </head>
    <body>
        <video autoplay controls id="first"></video>
        <video autoplay controls id="second"></video>
    </body>
</html>
